//
//  WaterFlowLayout.m
//  day21waterFlowLayout
//
//  Created by lanou3g on 15/5/13.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "WaterFlowLayout.h"

@interface WaterFlowLayout()//延展

@property(nonatomic,assign)NSUInteger numberOfItems;//item的数量
@property(nonatomic,strong)NSMutableArray*columnHeights;//保存每一列的总高度
@property(nonatomic,strong)NSMutableArray*itemAttributes;//保存每一个item的数据(frame数据)
//获取最长列的索引
-(NSInteger)p_indexForLongestColumn;
//获取最短列的索引
-(NSInteger)p_indexForShortestColumn;


@end

@implementation WaterFlowLayout

//懒加载初始化columnHeights
-(NSMutableArray*)columnHeights{
    if(_columnHeights==nil){
        self.columnHeights=[NSMutableArray array];
    }
    return _columnHeights;

}
-(NSMutableArray*)itemAttributes{
    if(_itemAttributes==nil){
        self.itemAttributes=[NSMutableArray array];
    
    }
    return _itemAttributes;

}
//获取最长列的索引
-(NSInteger)p_indexForLongestColumn{
//定义一个初始最大值
    NSInteger longestIndex=0;//最长列的索引
    CGFloat longestHeight=0;//最长列的数值
    for(NSInteger i=0;i<self.numberOfColumns;i++){
    //获取当前列的高度
        CGFloat currentHeight=[self.columnHeights[i] floatValue];
        //将每一列的高度跟设定的最大值比较
        if(currentHeight>longestHeight){
            longestHeight=currentHeight;
            longestIndex=i;
        }
        
    }
    return longestIndex;

}
//获取最短列的索引
-(NSInteger)p_indexForShortestColumn{

    //设定一个初始最小值
    NSInteger shortestIndex=0;//最短列的索引
    CGFloat shortestHeight=MAXFLOAT;//最短列的数值
    for(NSInteger i=0;i<self.numberOfColumns;i++){
        //获取当前列的高度
        CGFloat currentHeight=[self.columnHeights[i] floatValue];
        //将每一列的高度跟设定的最小值比较
        if(currentHeight<shortestHeight){
            shortestHeight=currentHeight;
            shortestIndex=i;
        }
        
    }
    return shortestIndex;

}

//计算item的位置
-(void)prepareLayout{
    [super prepareLayout];
//给columnHeights设置默认的初始数据(内边距)
    for(NSInteger i=0;i<self.numberOfColumns;i++){
    
        self.columnHeights[i]=@(self.sectionInsets.top);//
    
    }
//计算每个item的位置
    
//获取item的数量
    
    self.numberOfItems=[self.collectionView numberOfItemsInSection:0];
    //逐个设置item的位置
    for(NSUInteger i=0;i<self.numberOfItems;i++){
    //先找长度最小列
        NSInteger shortestColumnIndex=[self p_indexForShortestColumn];
        CGFloat shortestColumnHeight=[[self.columnHeights objectAtIndex:shortestColumnIndex] floatValue];
    //计算item的x  内边距left +(item的width +item的间隔)*列的索引
        
        CGFloat deltX=self.sectionInsets.left+(self.itemSize.width+self.interitemSpacing)*shortestColumnIndex;
        //计算item的Y 当前列的高度(最短列高度)+item行间距
        CGFloat deltY=shortestColumnHeight+_lineSpacing;
     //计算item的高度(根据model数据计算)
      //controller中处理数据源,layout负责布局
      //layout将indexPath(位置)传给controller,controller将数据计算后的结果返回给layout用于布局
        NSIndexPath *indexPath=[NSIndexPath indexPathForItem:i inSection:0];
       //接受计算后的item的高度
        CGFloat itemHeight=0;
        if(_delegate &&[_delegate respondsToSelector:@selector(heightForItemAtIndexPath:)]){
            itemHeight=[_delegate heightForItemAtIndexPath:indexPath];
        
        }
        //创建attributes对象,记录item的数据
        UICollectionViewLayoutAttributes*layoutAttributes=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        layoutAttributes.frame=CGRectMake(deltX, deltY, self.itemSize.width, itemHeight);
        
        [self.itemAttributes addObject:layoutAttributes];
        
       //修改当前列的高度
        self.columnHeights[shortestColumnIndex]=@(itemHeight+deltY);
        
    }
}



//设置collectionView最终的contentSize
-(CGSize)collectionViewContentSize{
//1.先找最长列的值
    NSInteger longestColumnIndex=[self p_indexForLongestColumn];
    //根据最长列的索引获取数据
    CGFloat longestColumnHeight=[[self.columnHeights objectAtIndex:longestColumnIndex] floatValue];
//2.修改contentSize(只修改高度,最长列的高度+底部的内边距)
    CGSize contentSize=self.collectionView.frame.size;
    contentSize.height=longestColumnHeight+self.sectionInsets.bottom;
    return  contentSize;
 
}
//itemAttributes保存每一个item的数据(frame)
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

    return self.itemAttributes;

}


@end
